<template>
  <div>
    <x-card>
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="请求模块">
                <a-input v-model="queryParam.request_modular" allow-clear placeholder="请输入请求模块"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="请求方法">
                <a-input v-model="queryParam.request_method" allow-clear placeholder="请输入请求方法"/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="10" :sm="24">
                <a-form-item label="操作时间">
                  <a-range-picker
                    v-model="queryParam.dates"
                    :show-time="{
                      hideDisabledOptions: true,
                      defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')],
                    }"
                    format="YYYY-MM-DD HH:mm:ss"
                  />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons"
                    :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :rowKey="(record) => record.id"
      >
        <template slot="operator" v-if="hasPerm('oplog:delete')">
          <a-popconfirm @confirm="() => sysOpLogDelete()" placement="top" title="确认清空日志？">
            <a-button>清空日志</a-button>
          </a-popconfirm>
        </template>
        <span slot="request_path" slot-scope="text">
          <ellipsis :length="15" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="action" slot-scope="text, record">
          <span slot="action">
            <a v-if="hasPerm('oplog:detail')" @click="$refs.OplogForm.details(record)">查看详情</a>
          </span>
           <a-divider type="vertical" v-if="hasPerm('oplog:detail') & hasPerm('oplog:delete')"/>
                      <a-popconfirm v-if="hasPerm('oplog:delete')" placement="topRight" title="确认删除？"
                                    @confirm="() => sysOpLogDelete(record)">
              <a style="color: red">删除</a>
            </a-popconfirm>
        </span>
      </s-table>
      <oplog-form ref="OplogForm"/>
    </a-card>
  </div>
</template>
<script>
  import { STable, Ellipsis, XCard } from '@/components'
  import * as api from '@/api/system/logManage'
  import OplogForm from './Form'
  import moment from 'moment'

  export default {
    components: {
      XCard,
      STable,
      Ellipsis,
      OplogForm
    },
    data() {
      return {
        advanced: false,
        queryParam: {},
        columns: [
          {
            title: '请求模块',
            dataIndex: 'request_modular'
          },
          {
            title: '请求地址',
            dataIndex: 'request_path',
            scopedSlots: { customRender: 'request_path' }
          },
          {
            title: '请求方式',
            dataIndex: 'request_method'
          },
          {
            title: 'ip',
            dataIndex: 'request_ip'
          },
          {
            title: '响应码',
            dataIndex: 'response_code',
          },
          {
            title: '操作时间',
            dataIndex: 'create_datetime'
          },
          {
            title: '操作人',
            dataIndex: 'creator_name'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return api.getPage(Object.assign(parameter, this.switchingDate())).then((res) => {
            return res.data
          })
        }
      }
    },
    created() {
      if (this.hasPerm('oplog:detail') || this.hasPerm('oplog:delete')) {
        this.columns.push({
          title: '操作',
          width: '300px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
    },
    methods: {
      moment,
      /**
       * 查询参数组装
       */
      switchingDate() {
        const dates = this.queryParam.dates
        if (dates != null) {
          this.queryParam.start_time = moment(dates[0]).format('YYYY-MM-DD HH:mm:ss')
          this.queryParam.end_time = moment(dates[1]).format('YYYY-MM-DD HH:mm:ss')
          if (dates.length < 1) {
            delete this.queryParam.start_time
            delete this.queryParam.end_time
          }
          this.queryParam.ordering = 'create_datetime'
        }
        const obj = JSON.parse(JSON.stringify(this.queryParam))
        delete obj.dates
        return obj
      },
      /**
       * 删除日志
       */
      sysOpLogDelete(record) {
        if (record) {
          api.deleteObj(record.id).then((res) => {
            this._handle_response(res)
          })
        } else {
          api.multipleDelete().then((res) => {
            this._handle_response(res)
          })
        }
      },
      _handle_response(res) {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh(true)
        } else {
          this.$message.error('删除失败：' + res.message)
        }
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      }
    }
  }
</script>
<style lang="less">
</style>
